<?
    $link = mysqli_connect("localhost","root","root","stu_info");
    $arr = $_GET;
    // var_dump( $arr); 
    // var_dump($sqlOp);
    if( $arr['op'] == 'add' ){
        $sql = "INSERT INTO stu (stuName,age,gender,score) VALUES ('{$arr['stuName']}',{$arr['age']},{$arr['gender']},{$arr['score']})";
        mysqli_query($link,$sql);
    }
    if( $arr['op'] == 'del' ){
        $sql = "DELETE FROM stu WHERE id = {$arr['id']}"; 
        mysqli_query($link,$sql);
    }
    if ( $arr['op'] == 'sort'){
        $str = " ORDER BY {$arr['by']} ASC";
    }
    
    $sqlOp = "SELECT * FROM stu ".$str ;
    $res = mysqli_query($link,$sqlOp);//当sql为查询语句的时候，res取到的是一个数据集
    // // $arr = mysqli_fetch_assoc($res); //每次arr只能取到res的一条数据  
    // // 需要做循环，将所有数据集都循环出来
    // // print_r($arr);
    $Data = [] ; //申明一个空数组
    while( $arr =  mysqli_fetch_assoc($res) ){//当arr从res数据集中取到的数据不为空时
        array_push($Data,$arr);  //将arr这一条数据push到空数组中
    } 
    
    $str = json_encode($Data);
    echo $str;
?>